package com.ctg.behavior.calc.impala.builder.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.event.CtCalcEventMeasureReport;
import com.ctg.behavior.calc.utils.CtCalcDaysUtils;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtAggregatorConsts;
import com.ctg.behavior.common.consts.CtFieldConsts;

@Component
public class CtEventMeasureSelectBuilder {

	public String doBuilder(CtCalcEventMeasureReport measure) {

		String unit = measure.getUnit();
		
		StringBuilder sbuf = new StringBuilder();

		// 统计量度：常量定义(AggregatorConsts)
		String aggregator = measure.getAggregator().toLowerCase();
		// 总和 均值 最大值.. 时出现事件类型 field:event.$Anything.$user_id
		// SELECT字段Field (如：事件级-event.$Anything.$lib, 或 用户级-user.$sex)
		String field = measure.getField();
		CtCalcPropertyType selectFieldProperty = CtCalcPropertyUtils.getValueType(field);
		// byFields (查询分组字段)
		List<String> byFields = measure.getByFields();
		List<String> byFieldsProperties = new ArrayList<>();
		for (String oneByField : byFields) {
			CtCalcPropertyType byFieldProperty = CtCalcPropertyUtils.getValueType(oneByField);
			String propField = byFieldProperty.getProperty();
			byFieldsProperties.add(propField);
		}

		// 公共SELECT字段：第一层、第二层公共部分
		StringBuilder byFieldsSelect = new StringBuilder();
		for (int pIndex = 0; pIndex < byFieldsProperties.size(); pIndex++) {
			String propField = byFieldsProperties.get(pIndex);
			if (pIndex > 0) {
				byFieldsSelect.append(" , ");
			}
			byFieldsSelect.append(propField).append(" AS ").append(propField);
		}

		switch (aggregator) {
		// 总次数
		case CtAggregatorConsts.KEY_GENERAL:
			// 过去7天总次数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_7:
			// 过去30天总次数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_30:
			// 当月总次数
		case CtAggregatorConsts.KEY_COUNT_CUR_MONTH:
			// 合计总次数
		case CtAggregatorConsts.KEY_ROLLUP_COUNT:
			if (CtFieldConsts.FIELD_ANYTHING.equals(measure.getEventId())) {
				sbuf.append(" SELECT ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
			} else {
				sbuf.append(" SELECT event AS event , ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
			}
			if(!byFieldsProperties.isEmpty()) {
				sbuf.append(" , ");
			}
			sbuf.append(" count(*) AS total ");
			return sbuf.toString();

		// 用户数
		case CtAggregatorConsts.KEY_UNIQUE:
			// 过去7天总用户数
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_7:
			// 过去30天总用户数
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_30:
			// 当月用户数
		case CtAggregatorConsts.KEY_UNIQ_CUR_MONTH:
			// 合计用户数
		case CtAggregatorConsts.KEY_ROLLUP_UNIQ:
			if (CtFieldConsts.FIELD_ANYTHING.equals(measure.getEventId())) {
				// 第一层SELECT(顶层)
				sbuf.append(" SELECT T1.byTime AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" count(*) AS total FROM ( ");
				// 第二层SELECT(子查询)
				sbuf.append(" SELECT ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" user_id, count(*) AS total ");
			} else {
				// 第一层SELECT(顶层)
				sbuf.append("SELECT T1.event AS event , T1.byTime AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , count(*) AS total FROM ( ");
				// 第二层SELECT(子查询)
				sbuf.append("SELECT event AS event ");
				sbuf.append(" , ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , user_id, count(*) AS total ");
			}
			return sbuf.toString();

		// 平均数
		case CtAggregatorConsts.KEY_AVERAGE:
			if (CtFieldConsts.FIELD_ANYTHING.equals(measure.getEventId())) {
				// 第一层SELECT(顶层)
				sbuf.append(" SELECT T1.byTime AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" AVG(T1.total) AS total FROM ( ");
				// 第二层SELECT(子查询)
				sbuf.append(" SELECT ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" user_id, count(*) AS total ");
			} else {
				// 第一层SELECT(顶层)
				sbuf.append("SELECT T1.event AS event , T1.byTime AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , AVG(T1.total) AS total FROM ( ");
				// 第二层SELECT(子查询)
				sbuf.append("SELECT event AS event ");
				sbuf.append(" , ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , user_id, count(*) AS total ");
			}
			return sbuf.toString();

		// 总和
		case CtAggregatorConsts.KEY_SUM:
			sbuf.append(" SELECT event AS event, ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
			sbuf.append(" , ").append(byFieldsSelect.toString());
			sbuf.append(" , SUM(").append(selectFieldProperty.getProperty()).append(") AS total");
			return sbuf.toString();

		// 均值
		case CtAggregatorConsts.KEY_AVG:
			sbuf.append(" SELECT event AS event, ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
			sbuf.append(" , ").append(byFieldsSelect.toString());
			sbuf.append(" , AVG(").append(selectFieldProperty.getProperty()).append(") AS total");
			return sbuf.toString();

		// 最大值
		case CtAggregatorConsts.KEY_MAX:
			sbuf.append(" SELECT event AS event, ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
			sbuf.append(" , ").append(byFieldsSelect.toString());
			sbuf.append(" , MAX(").append(selectFieldProperty.getProperty()).append(") AS total");
			return sbuf.toString();

		// 最小值
		case CtAggregatorConsts.KEY_MIN:
			sbuf.append(" SELECT event AS event, ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
			sbuf.append(" , ").append(byFieldsSelect.toString());
			sbuf.append(" , MIN(").append(selectFieldProperty.getProperty()).append(") AS total");
			return sbuf.toString();

		// 人均值
		case CtAggregatorConsts.KEY_UNIQAVG:

			if (CtFieldConsts.FIELD_ANYTHING.equals(measure.getEventId())) {
				// 第一层SELECT
				sbuf.append(" SELECT T1.byTime AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" AVG(T1.total) AS total FROM ( ");
				// 第二层SELECT
				sbuf.append(" SELECT ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" user_id, count(*) AS total ");
			} else {
				// 第一层SELECT
				sbuf.append("SELECT T1.event AS event , T1.byTime AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				sbuf.append(" , AVG(T1.total) AS total FROM ( ");
				// 第二层SELECT
				sbuf.append("SELECT event AS event , ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , user_id, count(*) AS total ");
			}
			return sbuf.toString();

		// 去重数
		case CtAggregatorConsts.KEY_UNIQCOUNT:

			if (CtFieldConsts.FIELD_ANYTHING.equals(measure.getEventId())) {
				// 第一层SELECT
				sbuf.append("SELECT T1.byTime AS byTime , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(" count(*) AS total FROM ( ");
				// 第二层SELECT
				sbuf.append("SELECT ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				sbuf.append(" , ").append(byFieldsSelect.toString());
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ");
				}
				sbuf.append(selectFieldProperty.getProperty()).append(" AS ").append(selectFieldProperty.getProperty());
				sbuf.append(" , ");
				
				sbuf.append(" count(*) AS total ");
			} else {
				// 第一层SELECT
				sbuf.append("SELECT T1.event AS event , T1.byTime AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , count(*) AS total FROM ( ");
				// 第二层SELECT
				sbuf.append("SELECT event AS event , ").append(CtCalcDaysUtils.getFieldOfByTime(unit)).append(" AS byTime ");
				if(!byFieldsProperties.isEmpty()) {
					sbuf.append(" , ").append(byFieldsSelect.toString());
				}
				sbuf.append(" , count(*) AS total ");
			}
			return sbuf.toString();
			
		}

		return "";

	}

}
